import vue from 'vue'
import VueRouter from 'vue-router'
import login from '../view/login'
import Home from '../view/Home'
import ElementUI from 'element-ui'

// import main from '../components/main'
// import add from '../components/add'
vue.use(VueRouter)


const routes = [{
        path: '/',
        component: Home
    },
    {
        path: '/login',
        component: login
    },

    {
        path: '/home',
        meta: {
            title: '首页'
        },
        component: Home,
        redirect: '/index',

        children: [
            {
                path: '/index',
                meta: {
                    title: '首页'
                },
                component: () => import('../components/Index')
            },
            //用户管理
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '用户管理'
                },
                component: {
                    render(c) {
                        return c('router-view')
                    }
                },
                // 重定向
                redirect: '/user/userList',
                children: [{
                        path: '/user/userlist',
                        meta: {
                            title: '用户列表'
                        },
                        component: () => import('../components/user/userList')
                    },
                    {
                        path: '/user/useradd',
                        meta: {
                            title: '用户添加'
                        },
                        component: () => import('../components/user/userAdd')
                    }
                ]
            },
            //分类管理
            {
                path: '/classfiy',
                meta: {
                    title: "分类管理",

                },
                component: () => import('../components/Classfiy')
            },
            //博客添加
            {
                path: '/blog/blogAdd',
                meta: {
                    title: "博客添加"
                },
                component: () => import("../components/blog/blogAdd")
            },
            //博客列表
            {
                path: '/blog/blogList',
                meta: {
                    title: "博客列表"
                },
                component: () => import("../components/blog/blogList")
            },
            //权限管理
            {
                path: '/permissions',
                meta: {
                    title: "账户安全"
                },
                component: () => import("../components/Permissions")
            },
            //账户安全
            {
                path: '/Security',
                meta: {
                    title: "账户安全"
                },
                component: () => import("../components/Security")
            }

        ]
    },


];
const router = new VueRouter({

    routes,
})
// 路由拦截
router.beforeEach((to, from, next) => {
    // 判断是否没有 本地登录信息，用户是否没有保持着登录状态
    if (!sessionStorage.getItem('account')) {
      // 地址不是 login 时跳转到 login 中
      if (to.path !== '/login') {
        ElementUI.Message({
          type: "error",
          message: "未登录！",
        })
        next('/login')
      }
    }
    // 判断用户是否有权限进入后台
    if (sessionStorage.getItem('permissions') == '2') {
      // 地址不是 login 时跳转到 login 中
      if (to.path !== '/login') {
        ElementUI.Message({
          type: "error",
          message: "权限不够！无法进入后台！",
        })
        next('/login')
      }
    }
    // 继续跳转
    next()
  })
  
  export default router;